<?php

namespace app\common\model;


use think\Model;
// use traits\model\SoftDelete;
class Tourist extends Model
{
    // use SoftDelete;
    protected $autoWriteTimestamp = false;

    // protected $deleteTime = 'delete_time';
    // 定义时间戳字段名
    protected $createTime = 'create_time';



    protected $rule = [
        'phone'    => 'number|length:11',
        'id_no'    => 'length:15,18'
    ];

    protected $msg = [
        'phone'           => '手机格式不正确',
        'id_no'           => '身份证格式不正确'
    ];

    public function setTourists(array $params)
    {
        $play_data = [];
        foreach ($params as $k => $v) {
            $play_data[$k]['username'] = $v['name'];
            $play_data[$k]['phone'] = $v['phone'];
            $play_data[$k]['id_no'] = $v['idCard'];
            $play_data[$k]['school'] = $v['schoolName'];
            $play_data[$k]['school_id'] = $v['schoolId'];
            $play_data[$k]['student_id'] = $v['studentId'];
        }
        return $play_data;
    }

    /***
     * @param array $params
     * @param int $uid
     * 添加游客信息
     */
    public function add(array $params, int $user_id,int $order_id)
    {
        foreach ($params as &$value){
            $value['user_id'] = $user_id;
            $value['order_id'] = $order_id;
        }

       $result = $this->validate($this->rule,$this->msg)
                      ->allowField(true)
                      ->saveAll($params);

        if(false === $result){
            // 验证失败 输出错误信息
            exception($this->getError());
        }
        return $result;
    }

}